package com.learning.servlet3x.spring;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import java.lang.reflect.Modifier;
import java.util.Set;

/**
 * ClassName: SpringServletContainerInitializer
 * Description: 模拟 Spring 初始化
 * Date: 2014/12/06  10:02 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
    @Override
    public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
        System.out.println(" 模拟 Spring 初始化");

        for (Class<?> clazz : classes) {
            if (!clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers()) && WebApplicationInitializer
                    .class.isAssignableFrom(clazz)) {
                try {
                    ((WebApplicationInitializer) clazz.newInstance()).onStartup(servletContext);
                } catch (Throwable ex) {
                    throw new ServletException("Failed to instantiate webApplicationInitializer class", ex);
                }
            }
        }
    }
}
